package server;

import intializer.ComClientChannelInitializer;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import listener.LoopClientSendMessageListener;

public class ComClient {

    private final String addr;
    private final int port;
    private final NioEventLoopGroup group = new NioEventLoopGroup();
    private final Bootstrap clientBootstrap;

    public ComClient(String addr, int port){
        this.addr = addr;
        this.port = port;
        this.clientBootstrap = new Bootstrap();
    }

    /**
     * 开启服务
     */
    public void start() throws InterruptedException {

        clientBootstrap.group(group)
                .channel(NioSocketChannel.class)
                .option(ChannelOption.SO_KEEPALIVE,true)
                .handler(new ComClientChannelInitializer());

        clientBootstrap.connect(addr, port)
                .addListener(new LoopClientSendMessageListener()).sync()
                .channel().closeFuture().sync();
    }

}
